package in.muton.edushare.dynamicWebResources;

import java.util.Date;

import org.apache.wicket.markup.html.DynamicWebResource;
import org.apache.wicket.protocol.http.WebResponse;
import org.apache.wicket.util.time.Time;

/**
 * Dynamic Resource representation for Excel sheet
 * @author naishe
 *
 */
public abstract class ExcelResource extends DynamicWebResource {

	
	private static final long serialVersionUID = 2719856532760589338L;

	@Override
	protected void setHeaders(WebResponse response) {
		super.setHeaders(response);
		response.setHeader("Content-Disposition", "attachment; filename=" + getFileName());
	}

	@Override
	protected ResourceState getResourceState() {
		return new ResourceState(){

			@Override
			public String getContentType() {
				return "application/x-ms-excel";
			}

			@Override
			public byte[] getData() {
				return getExcel();
			}
			
			@Override
			public int getLength() {
				return getExcel().length;
			}
			
			@Override
			public Time lastModifiedTime() {
				return Time.now();
			}

		};
	}
	
	/** To be overridden by the Excel file creator class */
	protected abstract String getFileName();
	protected abstract byte[] getExcel();

}
